package com.ease.framework.gef4p.editparts {
   import com.ease.common.models.IModel;
   import com.ease.framework.gef.EditPart;
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.commands.CreateEdgeCommand;
   import com.ease.framework.gef4p.commands.MoveEdgeSourceAnchorCommand;
   import com.ease.framework.gef4p.commands.MoveEdgeTargetAnchorCommand;
   import com.ease.framework.gef4p.commands.RemoveEdgeCommand;
   import com.ease.framework.gef4p.figures.ILabelFigureBox;
   import com.ease.framework.gef4p.figures.INodeFigure;
   import com.ease.framework.gef4p.figures.impl.edge.EdgeFigure;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.ICommand;

   import flash.events.ContextMenuEvent;
   import flash.geom.Point;
   import flash.ui.ContextMenuItem;

   public class EdgeEditPart extends EditPart {
      private var _source:NodeEditPart;

      private var _target:NodeEditPart;

      public function EdgeEditPart(obj:IModel) {
         super(obj);

      }

      public function setSource(source:NodeEditPart):void {
         _source = source;
         EdgeFigure(figure).source = _source.figure as INodeFigure;

         if (Edge(model).sourcePoint)
            EdgeFigure(figure).setSourceBend(Edge(model).sourcePoint.x, Edge(model).sourcePoint.y);

         source.addOutEdge(this);
      }

      public function setTarget(target:NodeEditPart):void {
         _target = target;
         EdgeFigure(figure).target = _target.figure as INodeFigure;

         if (Edge(model).targetPoint)
            EdgeFigure(figure).setTargetBend(Edge(model).targetPoint.x, Edge(model).targetPoint.y);

         target.addInEdge(this);

      }

      public function getSource():NodeEditPart {
         return _source;
      }

      public function getTarget():NodeEditPart {
         return _target;
      }

      override public function createCustomContextMenuItems():void {
         var menuItem_delete:ContextMenuItem = new ContextMenuItem("删除连线");
         menuItem_delete.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleRemove);
         this.pushCustomContextMenuItem(menuItem_delete);
      }

      protected function handleRemove(param1:ContextMenuEvent):void {
         var command:ICommand = new RemoveEdgeCommand(model as Edge);
         IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().push(command);
      }

      override public function doRefreashFigure():void {
         super.doRefreashFigure();

         var edge:Edge = Edge(model);
         ILabelFigureBox(figure).getLabelFigure().text = edge.name;
         EdgeFigure(figure).refresh();
      }

      public function createMoveSourceAnchorCommand(sourceNode:Node):ICommand {
         var command:ICommand = new MoveEdgeSourceAnchorCommand(Edge(model), sourceNode);
         return command;
      }

      public function createMoveTargetAnchorCommand(targetNode:Node):ICommand {
         var command:ICommand = new MoveEdgeTargetAnchorCommand(Edge(model), targetNode);
         return command;
      }

      override public function createNewCommand(pool:IModel, parent:IModel = null, point:Point = null):ICommand {
         var command:ICommand = new CreateEdgeCommand(model as Edge);
         return command;
      }

      override public function createRemoveCommand():ICommand {
         return new RemoveEdgeCommand(model as Edge);
      }
   }
}